/*
 *所有关于PurchaseWarehousing类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*PurchaseWarehousingService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Domain.IRepositories;
using VOL.Domain.IServices;
using VOL.Domain.Infrastructure;
using System;

namespace VOL.Domain.Services
{
    /// <summary>
    /// 采购入库
    /// </summary>
    public partial class PurchaseWarehousingService: IPurchaseWarehousingService
    {

        private readonly IBaseRepository<PurchaseWarehousing> _purchaseWarehousingRepository;

        public PurchaseWarehousingService(IBaseRepository<PurchaseWarehousing> purchaseWarehousingRepository)
        {
            _purchaseWarehousingRepository = purchaseWarehousingRepository;
        }
 
        /// <summary>
        /// 采购入库新增
        /// </summary>
        /// <param name="purchaseWarehousing"></param>
        /// <returns></returns>
        public async Task<int> AddPurchaseWarehousing(PurchaseWarehousing purchaseWarehousing)
        {
            return await _purchaseWarehousingRepository.Add(purchaseWarehousing);
        }
        /// <summary>
        /// 采购入库显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<PurchaseWarehousing>> GetAllPurchaseWarehousing(string? inboundListCode, string? inboundListName, DateTime? inboundListTime, string? informCode, int pageIndex = 1, int pageSize = 5)
        {
            var purchaseWarehousingList = _purchaseWarehousingRepository.GetAll();
            purchaseWarehousingList = purchaseWarehousingList.WhereIF(inboundListCode != null, m => m.InboundListCode.Contains(inboundListCode));
            purchaseWarehousingList = purchaseWarehousingList.WhereIF(inboundListName != null, m => m.InboundListName.Contains(inboundListName));
            purchaseWarehousingList = purchaseWarehousingList.WhereIF(inboundListTime != null, m => m.InboundListTime==inboundListTime);
            purchaseWarehousingList = purchaseWarehousingList.WhereIF(informCode != null, m => m.InformCode.Contains(informCode));
            var total = purchaseWarehousingList.Count();
            var items = purchaseWarehousingList.OrderBy(m => m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return purchaseWarehousingList.ToList();
        }
        /// <summary>
        /// 采购入库修改
        /// </summary>
        /// <param name="purchaseWarehousing"></param>
        /// <returns></returns>
        public async Task<int> UpdatePurchaseWarehousing(PurchaseWarehousing purchaseWarehousing)
        {
            return await _purchaseWarehousingRepository.Update(purchaseWarehousing);
        }
    

        /// <summary>
        /// 删除采购入库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int>  DeletePurchaseWarehousing(int id)
        {
            var inventoryManagement = (await _purchaseWarehousingRepository.FindAsync(m => m.Id == id)).FirstOrDefault();
            if (inventoryManagement == null) return 0;
            return await _purchaseWarehousingRepository.Delete(inventoryManagement);
        }
    }
}
